/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package objective;

/**
 *
 * @author daz
 */
public class AllDifferent implements IObjective {
    
    private int[] vectorVar;
    private int violateDegrees;
    private int vectorSize;
    
    public AllDifferent(int[] vectorVar) {
        this.vectorVar = vectorVar;
        this.vectorSize = (vectorVar == null ? 0 : vectorVar.length);
        violateDegrees = calculateViolation(vectorVar);
    }
    
    private int calculateViolation(int[] vectorVar) {
        if (vectorSize == 0 || vectorSize == 1) {
            return 0;
        }
        int result = 0;
        for (int i = 0; i < vectorSize; i++) {
            for (int j = 0; j < vectorSize; j++) {
                if (i != j && vectorVar[i] == vectorVar[j]) {
                    result++;
                }
            }
        }
        return result;
    }
    
    public void updateVector(int index, int newValue) {
        vectorVar[index] = newValue;
        violateDegrees = calculateViolation(vectorVar);
    }
    
    @Override
    public int getViolateDegrees() {
        return violateDegrees;
    }
    
    @Override
    public int getAssignDelta(int index, int newValue) {
        vectorVar[index] = newValue;
        int newViolateDegrees = calculateViolation(vectorVar);
        return newViolateDegrees - violateDegrees;
    }
}
